<template>
	<view v-if="appId !== 'wx8033fe9de99c902d'" class="pop-bg">
		<view class="pop-share-view">
			<button class="pop-button" open-type="share">分享给家长</button>
		</view>
		<view class="pop-line-short"></view>

		<image :src="QRCodeSrc" class="pop-qrcode" />

		<view class="pop-button" @click="qrCodeSaveClick">保存二维码</view>
		<view class="pop-line-short"></view>

		<view class="pop-input-address">
			邮箱地址:<input :value="mailAddress" class="pop-input" @input="emailInput" />
		</view>
		<view style="font-size: 20rpx;color: #666666;">
			(为保证测评结果邮件投递成功，请优先使用QQ邮箱)
		</view>
		<view class="pop-button" @click="emailClick">保存文档到邮箱</view>		
		
		<view class="pop-button" @click="saveEmailUrlToClipboard">复制报告下载地址</view>
		
		<view class="pop-line"></view>
		<view class="pop-cancel" @click="shareCancel">
			取消
		</view>
	</view>
	<view v-else class="pop-bg">
		<image :src="QRCodeSrc" class="pop-qrcode" />
		<view v-if="!parentGranted" style="display: flex;flex-direction: row;align-items: center;">
			<view>
				<view>同时推送到测评公众号</view>
				<view style="font-size: 20rpx;color: #666666;">
					(家长关注公众号后，可随时收到测评结果推送)
				</view>
			</view>
			<switch style="margin-left: 20rpx; transform:scale(0.7)" checked="true" color="#FACB2F" @change="checkChange" />
		</view>
		<view class="pop-button" @click="qrCodeSaveClick">保存</view>
		
		<view class="pop-line"></view>
		<view class="pop-cancel" @click="shareCancel">
			取消
		</view>
	</view>
</template>

<script>
	import utils from '@/utils/utils.js'
	import {
		pathToBase64,
		base64ToPath
	} from '@/js_sdk/gsq-image-tools/image-tools/index.js'
	export default {
		name: "AssessSaveView",
		props: {
			evaluateRecordId: {
				type: String,
				default() {
					return ''
				}
			},
			QRCodeSrc: {
				type: String,
				default() {
					return ''
				}
			},
			parentGranted: {
				type: Boolean,
				default() {
					return true;
				}
			}
		},
		data() {
			return {
				appId: getApp().globalData.appid,
				mailAddress: '',
				
				check : true,
			};
		},
		created() {
			let that = this;
			uni.getStorage({
				key: 'mailAddress',
				success: function(res) {
					that.mailAddress = res.data
				}
			});
		},
		onShareAppMessage(res) {
			if (res.from === 'button') { // 来自页面内分享按钮
				console.log(res.target)
			}
			return {
				title: this.name + '的测评',
				path: '/pages-assess/assess/assess-result/assess-result?evaluateRecordId=' + this.evaluateRecordId +
					'&isShared=true'
			}
		},
		methods: {
			emailInput(evt) {
				this.mailAddress = evt.detail.value;
			},
			saveEmailUrlToClipboard() {
				this.getReportWordUrl();
			},
			///发送邮箱
			emailClick() {
				if (!utils.checkEmail(this.mailAddress)) {
					uni.showToast({
						title: '邮箱格式错误',
						icon: 'none'
					});
					return;
				}
				uni.showLoading({})
				uni.setStorage({
					key: 'mailAddress',
					data: this.mailAddress,
				})
				this.$http.request({
					method: 'GET',
					url: '/eapi/operator/email_report',
					params: {
						evaluateRecordId: this.evaluateRecordId,
						mailAddress: this.mailAddress
					}
				}).then(res => {
					console.log(res)
					uni.hideLoading()
					if (res.code === '000000') {
						uni.showToast({
							title: '发送成功',
							icon: 'none'
						})
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(err => {
					console.log(err)
					uni.hideLoading()
					uni.showToast({
						title: err,
						icon: 'none'
					})
				});
			},
			///保存二维码
			qrCodeSaveClick() {
				// #ifdef MP-WEIXIN
				base64ToPath(this.QRCodeSrc)
					.then(path => {
						uni.saveImageToPhotosAlbum({
							filePath: path,
							success: function() {
								uni.showToast({
									title: '保存成功',
									icon: 'none'
								})
							},
							fail: function() {
								uni.showToast({
									title: '保存失败',
									icon: 'none'
								})
							}
						});
					})
					.catch(error => {
						uni.showToast({
							title: '保存失败',
							icon: 'none'
						})
					})
				// #endif
				// #ifdef H5
				base64ToPath(this.QRCodeSrc)
					.then(path => {
						uni.showToast({
							title: '保存成功',
							icon: 'none'
						})
					})
					.catch(error => {
						uni.showToast({
							title: '保存失败',
							icon: 'none'
						})
					})
				// #endif
			},
			
			checkChange(evt) {
				console.log(evt);
				this.check = evt.detail.value;
			},
			
			shareCancel() {
				this.$emit('closeClick');
			},
			
			getReportWordUrl() {
				uni.showLoading({})
				this.$http.request({
					method: 'GET',
					url: '/eapi/operator/upload_report',
					params: {
						evaluateRecordId: this.evaluateRecordId,
					}
				}).then(res => {
					console.log(res)
					uni.hideLoading()
					if (res.code === '000000') {
						uni.setClipboardData({
						    data: res.data,
						    success: function () {
						        uni.showToast({
						        	title: '复制成功',
									icon: 'none'
						        })
						    }
						});
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(err => {
					console.log(err)
					uni.hideLoading()
					uni.showToast({
						title: err,
						icon: 'none'
					})
				});
			},
		}
	}
</script>

<style lang="scss">
	.pop-bg {
		display: flex;
		flex-direction: column;
		background-color: #FFFFFF;
		align-items: center;
	}
	
	.pop-share-view {
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		width: 60%;
	
		margin-top: 40rpx;
	}
	
	.pop-button {
		width: 150px;
		height: 30px;
		background: linear-gradient(180deg, rgba(250, 204, 48, 1) 0%, rgba(252, 222, 122, 1) 100%);
		opacity: 1;
		border-radius: 4px;
	
		color: #FFFFFF;
		font-size: 30rpx;
		font-weight: bold;
	
		display: flex;
		justify-content: center;
		align-items: center;
	
		margin-bottom: 40rpx;
		margin-top: 30rpx;
		@include app-btn-background();
	}
	
	.pop-qrcode {
		width: 150px;
		height: 150px;
		margin-top: 20rpx;
	}
	
	.pop-input-address {
		display: flex;
		flex-direction: row;
		align-items: center;
	
		font-size: 15px;
		font-family: PingFang SC;
		font-weight: 400;
		line-height: 15px;
		color: rgba(101, 101, 101, 1);
		opacity: 1;
	
		margin-top: 40rpx;
		margin-bottom: 20rpx;
	}
	
	.pop-input {
		width: 150px;
		height: 30px;
		border: 1px solid rgba(230, 230, 230, 1);
		opacity: 1;
		margin-left: 10rpx;
		padding-left: 5rpx;
	}
	
	.pop-button {
		width: 150px;
		height: 30px;
		background: linear-gradient(180deg, rgba(250, 204, 48, 1) 0%, rgba(252, 222, 122, 1) 100%);
		opacity: 1;
		border-radius: 4px;
	
		color: #FFFFFF;
		font-size: 30rpx;
		font-weight: bold;
	
		display: flex;
		justify-content: center;
		align-items: center;
	
		margin-bottom: 40rpx;
		margin-top: 30rpx;
		@include app-btn-background();
	}
	
	.pop-cancel {
		color: #999999;
		color: 30rpx;
	
		height: 100rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	
	.pop-line {
		width: 375px;
		height: 2rpx;
		background-color: rgba(230, 230, 230, 1);
		opacity: 1;
	}
	
	.pop-line-short {
		width: 335px;
		height: 1rpx;
		background-color: rgba(230, 230, 230, 1);
		opacity: 1;
	}
</style>
